package com.StarStudio.Controller;

import com.StarStudio.Service.ComplaintService;
import com.StarStudio.Utils.QueryPageUtils;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.Entity.*;
import com.ruoyi.common.core.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/complaint")
@Api(tags = "商城投诉接口")
public class ComplaintController {

    @Autowired
    ComplaintService complaintService;

    /**
     * 新增投诉记录接口
     * @param complaint 投诉记录对象
     * @return 结果
     * @Author ChengLee
     * @Date 2023/03/22
     */
    @PostMapping
    @ApiOperation("新增投诉记录接口 -> 投诉类型、投诉者用户ID、用户名不能为空，投诉内容和投诉照片URL数组不能同时为空")
    public Result addComplaint(@RequestBody Complaint complaint){
        if (complaint.getComplaintType() == null || complaint.getComplaintUserId() == null || StringUtils.isBlank(complaint.getComplaintUserName()) ||
                (StringUtils.isBlank(complaint.getComplaintContent()) && StringUtils.isBlank(complaint.getComplaintPicture().toString()))) {
            return new Result(HttpStatusCode.BAD_REQUEST, MessageConstant.NOT_NULL_PARAMETER_ERROR);
        }
        try {
            complaintService.insertComplaint(complaint);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR,MessageConstant.ADD_COMPLAINT_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS,MessageConstant.ADD_COMPLAINT_SUCCESS);
    }

    /**
     * 修改投诉记录接口
     * @param complaint 投诉记录对象
     * @return 结果
     * @Author ChengLee
     * @Date 2023/03/22
     */
    @PutMapping
    @ApiOperation("修改投诉记录接口")
    public Result updateComplaint(@RequestBody Complaint complaint){
        if (complaint.getId() == null || (StringUtils.isBlank(complaint.getComplaintContent()) &&
                StringUtils.isBlank(complaint.getComplaintPicture().toString()))){
            return new Result(HttpStatusCode.BAD_REQUEST,MessageConstant.NOT_NULL_PARAMETER_ERROR);
        }
        try {
            complaintService.updateComplaint(complaint);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR,MessageConstant.UPDATE_COMPLAINT_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS,MessageConstant.UPDATE_COMPLAINT_SUCCESS);
    }

    /**
     * 查询投诉记录接口
     * @param complaint 投诉记录对象
     * @return 结果
     * @Author ChengLee
     * @Date 2023/03/22
     */
    @GetMapping
    @ApiOperation("查询投诉记录接口")
    public PageResult selectComplaint(Complaint complaint){
        QueryPageUtils.startPage();
        List<Complaint> complaintList;
        PageInfo pageInfo;
        long allRecordNumber;
        try {
            complaintList = complaintService.selectComplaint(complaint);
            allRecordNumber = complaintService.findAllRecordNumber();
            pageInfo = new PageInfo<>(complaintList);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(HttpStatusCode.ERROR,MessageConstant.GET_COMPLAINT_FAIL);
        }
        return new PageResult(HttpStatusCode.SUCCESS,MessageConstant.GET_COMPLAINT_SUCCESS, pageInfo.getTotal(), complaintList,allRecordNumber);
    }

    /**
     * 删除投诉记录接口
     * @param ids 记录ID数组
     * @return 结果
     * @Author ChengLee
     * @Date 2023/03/22
     */
    @DeleteMapping("/{ids}")
    @ApiOperation("删除投诉记录接口 -> 记录ID不能为空")
    public Result deleteComplaint(@PathVariable("ids") Long[] ids){
        if (ids == null || ids.length == 0){
            return new Result(HttpStatusCode.BAD_REQUEST,MessageConstant.NOT_NULL_PARAMETER_ERROR);
        }
        try {
            for (Long id : ids) {
                complaintService.deleteComplaint(id);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR,MessageConstant.DELETE_COMPLAINT_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS,MessageConstant.DELETE_COMPLAINT_SUCCESS);
    }

    /**
     * 获取最近的3条投诉记录接口
     * @return 结果
     * @Author ChengLee
     * @Date 2023/03/22
     */
    @GetMapping("/recentlyComplaint")
    @ApiOperation("获取最近的3条投诉记录")
    public Result selectRecentlyComplaint(){
        List<Complaint> complaintList;
        try {
            complaintList = complaintService.selectRecentlyComplaint();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR,MessageConstant.GET_RECENTLY_COMPLAINT_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS,MessageConstant.GET_RECENTLY_COMPLAINT_SUCCESS,complaintList);
    }

}
